package com.february.todo.DataBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBase extends SQLiteOpenHelper {

    private static final Integer Version = 1;

    public DataBase(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public DataBase(Context context, String name, int version) {
        this(context, name, null, version);
    }

    public DataBase(Context context, String name) {
        this(context, name, Version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i("message", "create database");
        String sql = "CREATE TABLE `todolist` (`id` INTEGER PRIMARY KEY AUTOINCREMENT," +
                "`date` varchar(10) NOT NULL," +
                "`title` varchar(255) NOT NULL," +
                "`detail` varchar(255)," +
                "`timeStart` varchar(5)," +
                "`timeEnd` varchar(5)," +
                "`photoPath` varchar(255)," +
                "`isFinished` INTEGER)";
        db.execSQL(sql);
        ContentValues cv = new ContentValues();
        cv.put("date", "2023-5-2");
        cv.put("title", "123");
        cv.put("detail", "456789123123");
        cv.put("timeStart", "16:00");
        cv.put("timeEnd", "17:00");
        cv.put("photoPath", "456123");
        for (int i = 0; i < 5; i++) {
            cv.put("isFinished", 0);
            cv.put("date", "2023-6-2" + String.valueOf(i));
            db.insert("todolist", null, cv);
            cv.put("isFinished", 1);
            cv.put("date", "2023-6-2" + String.valueOf(i));
            db.insert("todolist", null, cv);
            cv.put("isFinished", 0);
            cv.put("date", "2023-6-2" + String.valueOf(i));
            db.insert("todolist", null, cv);
        }

        for (int i = 0; i < 5; i++) {
            cv.put("isFinished", 0);
            cv.put("date", "2023-5-10" + String.valueOf(i));
            db.insert("todolist", null, cv);
            cv.put("isFinished", 1);
            cv.put("date", "2023-5-10" + String.valueOf(i));
            db.insert("todolist", null, cv);
            cv.put("isFinished", 0);
            cv.put("date", "2023-5-10" + String.valueOf(i));
            db.insert("todolist", null, cv);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("message", "数据更新");
    }
}
